const BaseCom = require("./baseCom");
const tools = require("./pub/tools");
cc.Class({
    extends: BaseCom,

    properties: {
    },

    onLoad(){
        //记录各元素原始位置;
        this.__initChildState();
    },

    __initChildState(){
        if(this.__mchildren) return;
        this.__mchildren = tools.recordChildren( this.node );
        this._afterInitChild && this._afterInitChild();
    },

    _appendMchildInfo( p ){
        p = p || {};
        for(let name in p){
            this.__mchildren[ name ] = this.__mchildren[ name ] || Object.assign({} , p[name] );
        }
    },

    //获取节点的初始记录数据
    getChildOriInfo( anmParam ){
        let name = tools.isType( anmParam , "String") ? anmParam : anmParam.name;
        this.__initChildState();
        let p = this.__mchildren[name] ? Object.assign({} , this.__mchildren[name] )  : null;
        if(!p){
            p = {};
            let node = $G.do("oneNode.get" , name );
            anmParam.conf.sprite && tools.setSprite( node , anmParam.conf.sprite);
            node.setPosition( anmParam.conf.pos || cc.v2(0,0));
            node.parent = anmParam.conf.parent || this.node;
            anmParam.conf.zIndex && (node.zIndex=anmParam.conf.zIndex);
            anmParam.conf.size && tools.setNodeSize(node , size );
            !tools.isType( anmParam.conf.opacity , "Undefined")  && (node.opacity = anmParam.conf.opacity);

            anmParam.conf.mask && 
            ( tools.setNodeSize(node , [node.parent.width , node.parent.height] )
             ,node.setPosition(cc.v2(0,0)) );
            anmParam.conf.blockInput && node.addComponent(cc.BlockInputEvents);
            !anmParam.conf.temp && this._appendMchildInfo( tools.recordChildren( this.node , name ) );

            p = {
                node : node,
                pos : node.getPosition(),
                size : node.getContentSize(),
                scale : node.scale,
                opacity : node.opacity
            }
        }else if( anmParam.conf ){
            let node = p.node;
            anmParam.conf.sprite && tools.setSprite( node , anmParam.conf.sprite);
            node.setPosition( anmParam.conf.pos || cc.v2(0,0));
            node.parent = anmParam.conf.parent || this.node;
            !tools.isType(anmParam.conf.zIndex , "Undefined") && (node.zIndex=anmParam.conf.zIndex);
            anmParam.conf.size && tools.setNodeSize(node , size );
            !tools.isType( anmParam.conf.opacity , "Undefined")  && (node.opacity = anmParam.conf.opacity);

            anmParam.conf.mask && 
            ( tools.setNodeSize(node , [node.parent.width , node.parent.height] )
             ,node.setPosition(cc.v2(0,0)) );
            if( anmParam.conf.blockInput ){
                if(!node.getComponent(cc.BlockInputEvents)){
                    node.addComponent(cc.BlockInputEvents);
                }
            }
            !anmParam.conf.temp && this._appendMchildInfo( tools.recordChildren( this.node , name ) );
        }
        
        return p;
    },

    _getAnimateNode( params ){

    },

    playChildAnimate( childNames , isUn ){
        childNames = childNames || [];
        isUn = $T.selectValue( isUn , false );
        let delay = 0;

        for(let i in childNames){
            let p = this.getChildOriInfo( childNames[i] );
            p.node.opacity = isUn ? p.node.opacity : 0;
            childNames[i].offsetPos && !isUn && p.node.setPosition( p.pos.add(childNames[i].offsetPos) );
            childNames[i].scale && !isUn && (p.node.scale = childNames[i].scale);

            delay = childNames[i].delay && childNames[i].delay>delay ? childNames[i].delay : delay;
            childNames[i].delay && childNames[i].delay>delay && childNames[i].time && (delay += childNames[i].time);
        }

        return new Promise((res)=>{
            //console.log(delay);
            $T.delay( res , true , delay);
            let z = $T.getMaxMinzindex( this.node );

            let run = ( index )=>{
                let cname = childNames[index];
                if(!cname || !cname.name){
                    return;
                }

                let fn = (i)=>{
                    let cname = childNames[i];
                    let p;
                    p = this.getChildOriInfo( cname );
                    let curPos = p.node.getPosition(),
                        curScale = p.node.scale,
                        curOpacity = p.node.opacity;
    
                    p.node.opacity = isUn ? curOpacity : p.opacity;
                    p.node.active = true;
                    p.node.zIndex = isUn ? parseInt(p.zIndex) : (parseInt(z.max) + parseInt(i));

                    let toparam ={position : isUn ? curPos.add(childNames[i].offsetPos || cc.v2(0,0)) :  p.pos};
                    p.scale && ( toparam.scale = (isUn && !tools.isType(childNames[i].scale ,'Undefined') )? childNames[i].scale : p.scale );
                    isUn && !tools.isType(childNames[i].opacity ,'Undefined') && ( toparam.opacity = childNames[i].opacity)
    

                    //console.log( toparam );
                    let anm = cc.tween( p.node );
                    anm.to( cname.time ? cname.time : 0 , toparam , cname.easing ? cname.easing : {});
                    if(cname.fn){
                        anm.call(()=>{
                            cname.fn(p.node);
                        })
                    }
                    if(cname.click){
                        p.node.off( cc.Node.EventType.TOUCH_END );
                        p.node.on( cc.Node.EventType.TOUCH_END , ()=>{
                            cname.click( p.node );
                        })
                    }
                    anm.start();
                }
                
                if( cname.delay ){
                    $T.delay( fn , index , cname.delay);
                }else{
                    fn(index)
                }

                run(index +1)
            }

            run(0);
        })
    }
});
